// pages/LoginAndRegisterModule/findPwd.js
import WxValidate from "../../utils/WxValidate";
const app = getApp()
Page({
    data: {
        auth_email: "",
        identify: "",
        auth_pwd: ""
    },
    onLoad: function (options) {
        //校验数据
        this.initValidate();
    },

    findPwd(e){
        let formData = this.data
        console.log(formData)
        if (!this.WxValidate.checkForm(formData)) {
            //表单元素验证不通过，此处给出相应提示
            let error = this.WxValidate.errorList[0]
            this.showModal(error)
            return false
        }
        //验证邮箱是否注册
        wx.request({
            url: `http://${app.globalData.target}//permission/isEmailExist`,
            data: {
              email:this.data.auth_email+'@hust.edu.cn'
            },
            method: 'GET',
            success: function(res){
              console.log(res)
              if (res.data.result != true) {
                //未被注册
                wx.showToast({
                  title: '邮箱未注册',
                  icon: 'error',
                  duration: 2000,
                  success: function(){
                    console.log('邮箱未注册')
                    //刷新验证码操作
                  }
                })
              }
            },
            fail: function() {
              console.log('fail to link')
            },
        })
        //发送修改密码请求
        wx.request({
          url: `http://${app.globalData.target}//permission/retrievePassword`,
          data: {
            email:this.data.auth_email+'@hust.edu.cn',
            newPassword: this.data.auth_pwd,
            verifyCode: this.data.identify
          },
          method: 'GET',
          success: function(res){
            console.log(res)
            if (res.data.code != 0) {
              wx.showToast({
                title: '操作失败',
                icon: 'error',
                duration: 2000
              })
            }
          },
          fail: function() {
            console.log('fail to link')
          }
        })
    },

    bindEmailBlur: function (e) {
        this.setData({
            auth_email: e.detail.value
        })
    },
    bindIdentifyBlur: function (e) {
        this.setData({
            identify: e.detail.value
        })
    },
    bindPwdBlur: function (e) {
        this.setData({
            auth_pwd: e.detail.value
        })
    },

    initValidate() {
        let rules = {
            auth_email: {required: true,},
            auth_pwd: {required: true,},
            identify: {required: true,},
        }
        //用于提示的信息
        let message = {
            auth_email: {required: '请输入邮箱',},
            auth_pwd: {required: "请输入新密码",},
            identify: {required: '请输入验证码',},
        }
        //实例化当前的验证规则和提示消息
        this.WxValidate = new WxValidate(rules, message);
      },
      /**错误提示 */
      showModal(error) {
        wx.showToast({
          title: error.msg,
          icon: 'none',
          duration: 1000
        })
      },

})